﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Configuration;
using System.Data;
using System.Text;

namespace IHISCETeim.Web.Asmx
{
    /// <summary>
    /// AutoCompleteRequest 的摘要说明
    /// </summary>
    public class AutoCompleteRequest : IHttpHandler
    {
        public enum ajaxmethod
        {

            /// <summary>
            /// 自动完成查询项目
            /// </summary>
            getautootallyproject,
            /// <summary>
            /// 自动完成问题代码
            /// </summary>
            getautoissuecode,
            /// <summary>
            /// 自动完成_获取工位
            /// </summary>
            getgwajax,
            /// <summary>
            /// 自动完成_获取作业内容
            /// </summary>
            getjobcontentajax,
            /// <summary>
            /// 自动完成_获取项目
            /// </summary>
            getprojectajax,
            /// <summary>
            /// 自动完成,获取品情单号
            /// </summary>
            autocompletegetallclaimsnumber,
            /// <summary>
            /// 自动完成,获取索赔单号
            /// </summary>
            autocompletegetallsuopeinum,
            /// <summary>
            /// 获取完检单或完成检查书问题列表 自动完成项
            /// </summary>
            getngexceptionrecordautocomplete,

            #region sap相关
            /// <summary>
            /// 获取物料
            /// </summary>
            getoitmlist,
            /// <summary>
            /// 获取代理商
            /// </summary>
            getagentlist,
            /// <summary>
            /// 获取供应商
            /// </summary>
            getsupplierlist,
            /// <summary>
            /// 获取供应商2 不使用jquery.autocomplete.js
            /// </summary>
            getsupplierlistsuggest,
            /// <summary>
            /// 获取最后一次采购价
            /// </summary>
            getoitmlastpurprice,
            #endregion

            /// <summary>
            /// 获取员工列表
            /// </summary>
            getuserlist
          

        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string method = context.Request["ajaxmethod"];

            ajaxmethod xmethod = (ajaxmethod)Enum.Parse(typeof(ajaxmethod), method.Trim().ToLower());
            string temp = "";
            switch (xmethod)
            {

                case ajaxmethod.getautootallyproject:
                    temp = getautootallyproject();
                    break;
                case ajaxmethod.getautoissuecode:
                    temp = getautoissuecode();
                    break;

                case ajaxmethod.getjobcontentajax:
                    temp = getjobcontentajax();
                    break;
                case ajaxmethod.getprojectajax:
                    temp = getprojectajax();
                    break;
                case ajaxmethod.autocompletegetallclaimsnumber:
                    temp = autocompletegetallclaimsnumber();
                    break;
                case ajaxmethod.autocompletegetallsuopeinum:
                    temp = autocompletegetallsuopeinum();
                    break;

                case ajaxmethod.getoitmlastpurprice:
                    temp = GetOITMLastPurPrice();
                    break;
                case ajaxmethod.getoitmlist:
                    temp = getoitmlist();
                    break;
                case ajaxmethod.getagentlist:
                    temp = getagentlist();
                    break;
                case ajaxmethod.getsupplierlist:
                    temp = GetSupplierList();
                    break;
                ///客户不是使用jquery.autocomplete.js
                case ajaxmethod.getngexceptionrecordautocomplete:
                    temp = getngexceptionrecordautocomplete();
                    break;
                case ajaxmethod.getsupplierlistsuggest:
                    temp = GetSupplierList();//getsupplierlistsuggest();
                    break;
                case ajaxmethod.getuserlist:
                    temp = getuserlist();
                    break;
               
                
            }
            //jsonp 支持
            string callbackfunname = GetRequestValue<string>("callbackparam", "");
            temp=string.IsNullOrEmpty(callbackfunname)?temp:callbackfunname + "('" + temp + "')";
            context.Response.Write(temp);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        
        #region 页面参数获取
        protected T GetRequestValue<T>(string name)
        {
            return HttpContext.Current.Request.GetRequestValue<T>(name);
        }

        protected T GetRequestValue<T>(string name, bool useDefValue, T defValue)
        {
            return HttpContext.Current.Request.GetRequestValue<T>(name, useDefValue, defValue);
        }
        protected T GetRequestValue<T>(string name, T defValue)
        {
            return HttpContext.Current.Request.GetRequestValue<T>(name, true, defValue);
        }
        #endregion

        #region 自动完成相关
        /// <summary>
        /// 查询关键字
        /// </summary>
        protected string key
        {
            get
            {
                return GetRequestValue<string>("q", true, "");
            }
        }
        /// <summary>
        /// 获取记录的前几条
        /// </summary>
        protected int toprow
        {
            get
            {

                return GetRequestValue<int>("limit", true, 20);
            }
        }

        protected string rowSplitTag
        {
            get
            {
                return ";";
            }
        }
        /// <summary>
        /// 自动完成查询项目
        /// </summary>
        private string getautootallyproject()
        {
            int itemType = GetRequestValue<int>("itemtype", 0);
            DataTable dt = OInspectionController.OTallyproject_GetALL(itemType, key, toprow);
            if (dt == null)
                return "";
            StringBuilder sb = new StringBuilder();
            foreach (DataRow DR in dt.Rows)
            {
                sb.AppendFormat("{0}|{1}{2}", DR["ItemCode"], DR["ItemName"], rowSplitTag);
                
            }
            return sb.ToString();
        }
        /// <summary>
        /// 自动完成问题代码
        /// </summary>
        private string getautoissuecode()
        {

            DataTable dt = OInspectionController.OIssueCode_GetALL(key, toprow);
            if (dt == null)
                return "";
            StringBuilder sb = new StringBuilder();
            foreach (DataRow DR in dt.Rows)
            {
                sb.AppendFormat("{0}|{1}{2}", DR["CodeTag"], DR["CodeName"], rowSplitTag);
               
            }
            return sb.ToString();
        }

        private string getjobcontentajax()
        {
            DataTable dt = OJobController.GetALLJobContent2(key, toprow);
            if (dt == null)
                return "";
            StringBuilder sb = new StringBuilder();
            foreach (DataRow DR in dt.Rows)
            {
                sb.AppendFormat("{0}|{1}{2}", DR["JobCode"], DR["JobTitle"], rowSplitTag);
            }
            return sb.ToString();
        }

        private string getprojectajax()
        {
            DataTable dt = OProjectController.GetProjectAjax(key, toprow);
            if (dt == null)
                return "";
            StringBuilder sb = new StringBuilder();
            foreach (DataRow DR in dt.Rows)
            {
                sb.AppendFormat("{0}|{1}{2}", DR["PName"], DR["MachineType"], rowSplitTag);
            }
            return sb.ToString();
        }

        private string autocompletegetallclaimsnumber()
        {
            
            DataTable dt = UClaimDemageControlController.GetALLClaimsNumber(key, toprow);
            if (dt == null)
                return "";
            StringBuilder sb = new StringBuilder();
            foreach (DataRow DR in dt.Rows)
            {
                sb.AppendFormat("{0}|{1}{2}", DR["ClaimsNumber"], DR["SerialNumber"], rowSplitTag);
              
            }

            
            return sb.ToString();
        }
        private string autocompletegetallsuopeinum()
        {
            DataTable dt = UClaimDemageControlController.GetALLSuoPeiNum(key, toprow);
            if (dt == null)
                return "";
            StringBuilder sb = new StringBuilder();
            foreach (DataRow DR in dt.Rows)
            {
                sb.AppendFormat("{0}|{1}{2}", DR["SuoPeiNum"], DR["ClaimsNumber"], rowSplitTag);
              
            }
            return sb.ToString();
        }
    
        private string getngexceptionrecordautocomplete()
        {
          //  string inputvalue = GetRequestValue<string>("inputValue", "");
            DataTable dt = OInspectionController.GetNGExceptionrecordList(key, toprow);
            if (dt == null)
                return "";

            StringBuilder sb = new StringBuilder();
            foreach (DataRow DR in dt.Rows)
            {
                sb.AppendFormat("{0}|{1}{2}", DR["Exceptionrecord"], "", rowSplitTag);

            }
            return sb.ToString();
            //JsonSerializerHelper js = new JsonSerializerHelper();
            //return js.Serialize(dt);

        }
        #endregion
        
        #region sap相关自动完成
        /// <summary>
        /// 物料查询
        /// </summary>
        /// <returns></returns>
        private string getoitmlist()
        {
            string showtype = GetRequestValue<string>("showtype", "");
            DataTable dt = null;
            if (showtype == "")
            {
                dt = SBOController.GetOITMList(key, toprow);
            }
            if (showtype == "101")
            {
                dt = SBOController.GetYanCaiLiaoOITM(key, toprow);
            }
            if (showtype == "102")
            {
                dt = SBOController.GetPaiJinOITMList(key, toprow);
            }


            if (dt == null)
                return "";

            StringBuilder sb = new StringBuilder();
            foreach (DataRow DR in dt.Rows)
            {
                sb.AppendFormat("{0}|{1}{2}", DR["itemcode"], DR["itemname"],rowSplitTag);
                //Response.Write(string.Format("{0}|{1}\n", DR["itemcode"], DR["itemname"]));
            }
            return sb.ToString();
        }
        /// <summary>
        /// 代理商查询
        /// </summary>
        /// <returns></returns>
        private string getagentlist()
        {
            DataTable dt = SBOController.GetAgentList(key, toprow);
            if (dt == null)
                return "";
            string showtype = GetRequestValue<string>("showtype", "address");

            StringBuilder sb = new StringBuilder();
            foreach (DataRow dr in dt.Rows)
            {
                sb.AppendFormat("{0}|{1}{2}", (string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"] : dr["AliasName"]), dr[showtype],rowSplitTag);
                //Response.Write(string.Format("{0}|{1}\n", DR["itemcode"], DR["itemname"]));
            }
            return sb.ToString();
        }

        /// <summary>
        /// 供应商查询
        /// </summary>
        /// <returns></returns>
        private string GetSupplierList()
        {
            DataTable dt = SBOController.GetSupplierList(key, toprow);
            if (dt == null)
                return "";
            string showtype = GetRequestValue<string>("showtype", "CardName");

            StringBuilder sb = new StringBuilder();
            foreach (DataRow dr in dt.Rows)
            {
                sb.AppendFormat("{0}|{1}{2}", (string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"] : dr["AliasName"]), dr[showtype], rowSplitTag);
                //Response.Write(string.Format("{0}|{1}\n", DR["itemcode"], DR["itemname"]));
            }
            return sb.ToString();
        }
        private string getsupplierlistsuggest()
        {
            string inputvalue = GetRequestValue<string>("inputValue", "");
            DataTable dt = SBOController.GetSupplierList(inputvalue, toprow);
            if (dt == null)
                return "";
            string showtype = GetRequestValue<string>("showtype", "CardName");

            StringBuilder sb = new StringBuilder();
            sb.Append("[");
            int rowcount = dt.Rows.Count;
            int currrowinex = 0;
            foreach (DataRow dr in dt.Rows)
            {
                currrowinex++;
                //{"id":"1", "orgName":"技术部", "orgNum":"1001"}
                sb.AppendFormat("{{ \"AliasName\":\"{0}\"}}", (string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"] : dr["AliasName"]));
                //  sb.AppendFormat("{0}|{1}\n", (string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"] : dr["AliasName"]), dr[showtype]);
                //Response.Write(string.Format("{0}|{1}\n", DR["itemcode"], DR["itemname"]));
                if (currrowinex != rowcount)
                    sb.Append(",");
            }

            sb.Append("]");
            return sb.ToString();
        }
        /// <summary>
        /// 获取最后一次采购价
        /// </summary>
        /// <returns></returns>
        private string GetOITMLastPurPrice()
        {
            string itemcode = GetRequestValue<string>("itemcode", "");
            return SBOController.GetOITMLastPurPrice(itemcode);
        }
        #endregion

        private string getuserlist()
        {
           // DataTable dt = SBOController.GetSupplierList(key, toprow);
            int TotalCount=0;
            DataTable dt = OUserController.GetPagination(key, "", 1, 1, 999999, " username desc ", ref TotalCount);
            if (dt == null)
                return "";
            StringBuilder sb = new StringBuilder();
            foreach (DataRow dr in dt.Rows)
            {
                sb.AppendFormat("{0}|{1}{2}", dr["RealName"], dr["UserName"], rowSplitTag);
                
            }
            return sb.ToString();
        }

        
    }
}